package com.ddwl.user.controller;

import com.alibaba.fastjson.JSON;
import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.common.exception.ParamException;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.order.DistOrderBo;
import com.ddwl.schema.bo.user.LowerUserDistQueryBo;
import com.ddwl.schema.bo.user.UserAgentQueryBo;
import com.ddwl.schema.bo.user.UserDistQueryBo;
import com.ddwl.schema.bo.user.UserDistRegionAddBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.dist.UserInviteCountVo;
import com.ddwl.schema.vo.user.AgentUserInfoVo;
import com.ddwl.schema.vo.user.ShareCodeInfoVo;
import com.ddwl.schema.vo.user.UserDistInfoVo;
import com.ddwl.schema.vo.user.UserDistListVo;
import com.ddwl.user.service.UserDistService;
import com.ddwl.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 类DistUserController.java的实现描述：分销用户操作相关接口
 *
 * @author zach
 * @Date :2020-09-02 11:13.
 */
@Api(tags = "分销用户操作相关接口")
@RequestMapping("/distUser")
@RestController
@Slf4j
@RequiredArgsConstructor
public class DistUserController extends BaseController {

    private final UserDistService userDistService;
    private final UserService userService;

    @PostMapping("/userLower")
    @ApiOperation("APP用户查询团队分销员")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<PageVo<UserDistListVo>> userLower(@RequestBody PageBo<LowerUserDistQueryBo> pageBo) {
        LowerUserDistQueryBo param = Optional.ofNullable(pageBo.getParam()).orElse(new LowerUserDistQueryBo());
        param.setUserId(getUserId());
        pageBo.setParam(param);
        return RestVo.ok(userDistService.userLower(pageBo));
    }

    @PostMapping("/page")
    @ApiOperation("查询分销用户")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.SHOP_CONSOLE})
    public RestVo<PageVo<UserDistListVo>> queryUserPage(@RequestBody PageBo<UserDistQueryBo> pageBo) {
        if (null == pageBo.getParam()) {
            pageBo.setParam(new UserDistQueryBo());
        }
        if (BaseEnum.ProductEnum.SHOP_CONSOLE.getName().equals(getProduct())) {
            pageBo.getParam().setMasterUser(getUserId());
        }
        return RestVo.ok(userDistService.userDistPage(pageBo));
    }

    @GetMapping("/info")
    @ApiOperation("查询分销用户详情")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo<UserDistInfoVo> getUserInfo(@RequestParam("userId") String userId) {
        return RestVo.ok(userDistService.getUserDistById(userId));
    }

    @GetMapping("/profile")
    @ApiOperation("查询分销用户详情")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO})
    public RestVo<UserDistInfoVo> getProfile() {
        return RestVo.ok(userDistService.getUserDistById(getUserId()));
    }

    @PostMapping("/add")
    @ApiOperation("新增区域合伙人")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo addUserDistRegion(@Valid @RequestBody UserDistRegionAddBo distRegionAddBo){
        userDistService.userDistAdd(distRegionAddBo);
        return RestVo.ok();
    }

    @PostMapping("/inviteCode/bind")
    @ApiOperation("绑定邀请码")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO})
    public RestVo bindInviteCode(@RequestParam String inviteCode) {
        userService.bindInviteCode(inviteCode,getUserId());
        return RestVo.ok();
    }

    @PostMapping("/userLowerIds")
    @ApiOperation(value = "APP查询下级分销员的Id",hidden = true)
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<String>> queryUserLowerIds(String userId) {
        return RestVo.ok(userDistService.queryUserLowerIds(userId));
    }

    @PostMapping("/userInviteCount")
    @ApiOperation(value = "APP用户当天邀请的用户统计信息",hidden = true)
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<UserInviteCountVo> getInviteCountVo(String userId) {
        return RestVo.ok(userDistService.queryUserInviteCount(userId));
    }

    @PostMapping("/userDistStatistical")
    @ApiOperation(value = "用户分销统计信息更新",hidden = true)
    @Auth(product = BaseEnum.ProductEnum.API)
    public void updateUserDistStatistical(@RequestBody List<DistOrderBo> distOrderBos){
        log.info("/userDistStatistical requestBody : {}", JSON.toJSONString(distOrderBos));
        userDistService.updateUserDistStatistical(distOrderBos);
    }
    @PostMapping("/upToRegionBoss")
    @ApiOperation(value = "升级为区域经理")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo upToRegionBoss(@Valid @RequestBody UserDistRegionAddBo bo){
        log.info("/upToRegionBoss requestBody : {}", JSON.toJSONString(bo));
        userDistService.upToRegionBoss(bo);
        return RestVo.ok();
    }

    @GetMapping("/getUserShareCode")
    @ApiOperation(value = "获取用户分享码")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<String> getUserShareCode(@RequestParam String userId){
        return RestVo.ok(userDistService.getUserShareCode(userId));
    }

    @GetMapping("/getShareCodeInfo")
    @ApiOperation(value = "获取分享码信息")
    @Auth(required = false, product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<ShareCodeInfoVo> getShareCodeInfo(@RequestParam(required = false) String scene, @RequestParam(required = false) String userId) {
        return RestVo.ok(userDistService.getShareCodeInfo(Optional.ofNullable(scene).orElse(userId)));
    }

    @PostMapping("/subAgent")
    @ApiOperation("查询下级代理")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<PageVo<AgentUserInfoVo>> subAgent(@Valid @RequestBody PageBo<UserAgentQueryBo> pageBo) {
        ParamException.isNull(pageBo.getParam(), "查询条件不能为空");
        UserAgentQueryBo bo = pageBo.getParam();
        if (!StringUtils.hasText(bo.getUserId())) {
            bo.setUserId(getUserId());
        }
        return RestVo.ok(userDistService.subAgent(pageBo));
    }

}
